package com.SmartCare.controller.manager;

import com.SmartCare.common.Result;
import com.SmartCare.pojo.role.ElderlyInformation;
import com.SmartCare.service.role.ElderlyInformationService;
import com.SmartCare.service.ImageService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 老人信息表 前端控制器
 */
@RestController
@RequestMapping("/elderly-information")
public class ElderlyInformationController {

    @Resource
    private ElderlyInformationService elderlyInformationService;

    @Resource
    private ImageService imageService;

    @Value("${files.upload.path}/elderlyImg/")
    private String fileUploadPath;

    //根据养老院名获取老人相关信息
    @GetMapping("/query")
    public Result queryElderData(@RequestParam String name) {
        return elderlyInformationService.queryElderData(name);
    }

    //搜索老人具体列表信息
    @GetMapping("/search")
    public Result search(@RequestParam Integer pageNum,
                         @RequestParam Integer pageSize,
                         @RequestParam String keyword,
                         @RequestParam String type,
                         @RequestParam String name) {
        return elderlyInformationService.managerGetList(pageNum, pageSize, keyword, type, name);
    }

    //添加图片
    @PostMapping("/elder/upload")
    @Operation(summary = "上传图片文件", description = "将返回该图片的下载地址")
    public Result addImage(@RequestParam List<MultipartFile> file) {
        return imageService.upload(file, fileUploadPath, "elderly-information/elder");
    }

    //下载图片
    @GetMapping("/elder/{fileUuid}")
    @Operation(summary = "下载图片文件", description = "直接访问该路径可下载图片")
    public void download(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
        imageService.download(fileUuid, fileUploadPath, response);
    }

    //更新老人
    @PutMapping("/update-elder")
    public Result updateElder(@RequestBody ElderlyInformation elderlyInformation) {
        return elderlyInformationService.updateElder(elderlyInformation);
    }

    //添加老人
    @PostMapping("/add-elder")
    public Result addElder(@RequestBody ElderlyInformation elderlyInformation) {
        return elderlyInformationService.addElder(elderlyInformation);
    }

    //删除老人
    @DeleteMapping("/delete-elder")
    public Result deleteElder(@RequestParam Long id) {
        return elderlyInformationService.deleteElder(id);
    }

    //导入
    @PostMapping("/import/{id}")
    public Result importElder(@RequestParam MultipartFile file, @PathVariable Long id){
        return elderlyInformationService.importElder(file, id);
    }

    //导出
    @GetMapping("/export/{id}")
    public void export(HttpServletResponse response, @PathVariable String id){
        elderlyInformationService.export(response, id);
    }

    //获取没有药单的老人列表
    @GetMapping("/query-no-ml")
    public Result getNoMedicationListElders(@RequestParam String careHomeName){
        return elderlyInformationService.getElders(careHomeName, false);
    }

    //获取有药单的老人列表
    @GetMapping("/query-has-ml")
    public Result getHasMedicationListElders(@RequestParam String careHomeName){
        return elderlyInformationService.getElders(careHomeName, true);
    }

    //根据养老院名获取老人相关信息
    @GetMapping("/query-elders")
    public Result queryElderByCareHome(@RequestParam String name) {
        return elderlyInformationService.getElderListByName(name);
    }
}
